using System.Collections;
using UnityEngine;

public class HighlightDisplayObject : MonoBehaviour
{
	public UIWidget itemToHighlight;

	public UIWidget highlightElement;

	public Transform transformToParentTo;

	public UISprite highlightCircle;

	public UISprite pointer;

	public TweenAlpha bck;

	public float markerDuration = 0.5f;

	public float markerDelay = 0.5f;

	public float markerScale = 4f;

	public float pointerSpeed = 0.5f;

	public float maxDistance = 50f;

	private Coroutine circleRoutine;

	private Coroutine pointerRoutine;

	private Transform highlightItemParent;

	private int highlightCircleOriginalDepth;

	private int highlightPointerOriginalDepth;

	private void OnEnable()
	{
		if (!(itemToHighlight == null))
		{
			highlightCircleOriginalDepth = highlightCircle.depth;
			highlightPointerOriginalDepth = pointer.depth;
			highlightItemParent = itemToHighlight.transform.parent;
			itemToHighlight.transform.parent = transformToParentTo;
			highlightCircle.depth = highlightElement.depth - 1;
			highlightCircle.gameObject.SetActive(false);
			highlightCircle.gameObject.SetActive(true);
			pointer.depth = highlightElement.depth - 1;
			pointer.gameObject.SetActive(false);
			pointer.gameObject.SetActive(true);
			itemToHighlight.gameObject.SetActive(false);
			itemToHighlight.gameObject.SetActive(true);
			circleRoutine = StartCoroutine(DoMarkAsCurrent());
			pointerRoutine = StartCoroutine(DoBouncePointer());
			bck.ResetToBeginning();
			bck.Play();
		}
	}

	private void OnDisable()
	{
		if (!(itemToHighlight == null) && !(highlightItemParent == null))
		{
			StopCoroutine(circleRoutine);
			circleRoutine = null;
			StopCoroutine(pointerRoutine);
			pointerRoutine = null;
			itemToHighlight.transform.parent = highlightItemParent;
			highlightCircle.depth = highlightCircleOriginalDepth;
			highlightCircle.gameObject.SetActive(false);
			highlightCircle.gameObject.SetActive(true);
			pointer.depth = highlightPointerOriginalDepth;
			pointer.gameObject.SetActive(false);
			pointer.gameObject.SetActive(true);
			itemToHighlight.gameObject.SetActive(false);
			itemToHighlight.gameObject.SetActive(true);
			highlightItemParent = null;
			itemToHighlight = null;
		}
	}

	private IEnumerator DoMarkAsCurrent()
	{
		Vector3 startScale = Vector3.one * 0f;
		Vector3 endScale = Vector3.one * markerScale;
		float startAlpha = 1f;
		int endAlpha = 0;
		while (true)
		{
			float markerTime = 0f;
			while (markerTime < markerDuration)
			{
				markerTime += RealTime.deltaTime;
				float normTime = markerTime / markerDuration;
				highlightCircle.cachedTransform.localScale = Vector3.Lerp(startScale, endScale, normTime);
				highlightCircle.alpha = Mathf.Lerp(startAlpha, endAlpha, normTime);
				yield return null;
			}
			highlightCircle.cachedTransform.localScale = endScale;
			highlightCircle.alpha = endAlpha;
			yield return new WaitForSeconds(markerDelay);
		}
	}

	private IEnumerator DoBouncePointer()
	{
		float pointerTime = 0f;
		Vector3 startPos = pointer.transform.localPosition;
		while (true)
		{
			float yDist = Mathf.PingPong(pointerTime * pointerSpeed, maxDistance);
			Vector3 newPos = startPos;
			newPos.y += yDist;
			pointer.transform.localPosition = newPos;
			pointerTime += RealTime.deltaTime;
			yield return null;
		}
	}
}
